package cn.itcast.contorller.cargo;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.ContractService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseContorller {
    @Reference
    private ContractService contractService;
    @Autowired
    private DownloadUtil downloadUtil;
    @RequestMapping(value = "/list",name ="合同的全查")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        ContractExample contractExample=new ContractExample();
        /*设置排序查询*/
        contractExample.setOrderByClause("create_time desc");
        /*设置条件查询的对象*/
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andCompanyIdEqualTo(company_id);
        // 细颗粒度的数据控制
        Integer degree = user.getDegree();
        if (degree==4){
            criteria.andCreateByEqualTo(user.getId());//根据登录用户
        }else if (degree==3){
            criteria.andCreateDeptEqualTo(user.getDeptId());//根据登录用户的部门
        }
        /*条件的查询*/
        PageInfo all = contractService.findAll(contractExample,pageNum,pageSize);
        request.setAttribute("pb",all);
        return "cargo/contract/contract-list";
    }
    @RequestMapping(value = "/toAdd",name = "去新增购销合同页面")
    public String toAdd(){
        return "/cargo/contract/contract-add";
    }
    @RequestMapping(value ="/edit",name = "新增购销合同/修改新增合同")
    public String edit(Contract contract){
        /*设置属于那个企业的合同*/
        contract.setCompanyId(company_id);
        contract.setCompanyName(company_name);
        if (UtilFuns.isEmpty(contract.getId())){
          //新增购销合同
            contractService.save(contract);
        }else {
        /*修改新增合同*/
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping(value ="/toUpdate",name = "去合同修改页面")
    public String toUpdate(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "/cargo/contract/contract-update";
    }
    @RequestMapping(value = "/delete",name = "删除购销合同")
    public String delete(String id){
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping(value = "/toView",name = "合同的查看")
    public String toView(String id){
        Contract byId = contractService.findById(id);
        request.setAttribute("contract",byId);
        return "/cargo/contract/contract-view";
    }
    @RequestMapping(value = "/submit",name = "修改合同的状态,去提交,把草稿状态修改为准备提交")
    public String submit(String id){
        Contract contract = contractService.findById(id);
        if (contract.getState()==0){
            contract.setState(1);
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping(value = "/cancel",name = "取消准备上报合同")
    public String cancel(String id){
        Contract contract = contractService.findById(id);
        if (contract.getState()==1){
            contract.setState(0);
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping(value = "/print",name = "到导出合同页面")
    public String print(){
        return "cargo/print/contract-print";
    }
    @RequestMapping(value = "/printExcel",name = "导出合同到excel表")
    public String printExcel(String inputDate) throws IOException {
        //1 根据船期获取到excel需要的数据
       List<ContractProductVo> list= contractService.findExcelData(company_id,inputDate);
        System.out.println(list.size());
        //2 将数据填充到准备好的模板中
        ServletContext servletContext = session.getServletContext();
        /*返回一个绝对路径*/
        String realPath = servletContext.getRealPath("/make/xlsprint/tOUTPRODUCT.xlsx");
        //读取指定路径的excel工作薄
        Workbook wk=new XSSFWorkbook(realPath);
        /*获取第一页*/
        Sheet sheet = wk.getSheetAt(0);
        int rowIndex=0;
        Row row;
        Cell cell;
        //第一行 数据的写入
        row= sheet.getRow(rowIndex++);
        cell = row.getCell(1);//获取第一个单元格
        inputDate.replace("-0","-").replace("-","年");
        cell.setCellValue(inputDate+"月份出货表");//设置单元格的内容
        //第二行 跳过
        rowIndex++;
        //把数据写入第三行
       row=sheet.getRow(rowIndex++);
       /*单元格的样式*/
        CellStyle[] cellStyles=new CellStyle[9];
        for (int i =1;i<row.getLastCellNum();i++) {
            // 获取每个单元格的样式
             cell = row.getCell(i);
            cellStyles[i]=cell.getCellStyle();
        }
        //填充数据 有多少对象就 填充多少行的数据
        if(list.size()>0){
            for (ContractProductVo contractProductVo:list) {
                //获取行
                row=sheet.createRow(rowIndex++);
                //设置每行单元格
                cell=row.createCell(1);
                //给单元格内容
                cell.setCellValue(contractProductVo.getCustomName());
                //给单元格样式
                cell.setCellStyle(cellStyles[1]);

                //设置每行单元格
                cell=row.createCell(2);
                //给单元格内容
                cell.setCellValue(contractProductVo.getContractNo());
                //给单元格样式
                cell.setCellStyle(cellStyles[2]);

                //设置每行单元格
                cell=row.createCell(3);
                //给单元格内容
                cell.setCellValue(contractProductVo.getProductNo());
                //给单元格样式
                cell.setCellStyle(cellStyles[3]);

                //设置每行单元格
                cell=row.createCell(4);
                //给单元格内容
                cell.setCellValue(contractProductVo.getCnumber());
                //给单元格样式
                cell.setCellStyle(cellStyles[4]);

                //设置每行单元格
                cell=row.createCell(5);
                //给单元格内容
                cell.setCellValue(contractProductVo.getFactoryName());
                //给单元格样式
                cell.setCellStyle(cellStyles[5]);

                //设置每行单元格
                cell=row.createCell(6);
                //给单元格内容
                cell.setCellValue(contractProductVo.getDeliveryPeriod());
                //给单元格样式
                cell.setCellStyle(cellStyles[6]);

                //设置每行单元格
                cell=row.createCell(7);
                //给单元格内容
                cell.setCellValue(contractProductVo.getShipTime());
                //给单元格样式
                cell.setCellStyle(cellStyles[7]);

                //设置每行单元格
                cell=row.createCell(8);
                //给单元格内容
                cell.setCellValue(contractProductVo.getTradeTerms());
                //给单元格样式
                cell.setCellStyle(cellStyles[8]);
            }
        }
        ByteArrayOutputStream ou=new ByteArrayOutputStream();
        wk.write(ou);
        downloadUtil.download(ou,response,"zi定义.xlsx");
        return "";
    }

}
